﻿namespace CatClient.Message.Spi.Codec
{
    using System;
    using System.IO;
    using System.Text;

    public class ChannelBuffer : IDisposable
    {
        private readonly MemoryStream _memoryBuf;
        private readonly BinaryWriter _binaryWriter;

        public ChannelBuffer(int capacity)
        {
            this._memoryBuf = new MemoryStream(capacity);
            this._binaryWriter = new BinaryWriter(this._memoryBuf, Encoding.UTF8);
        }

        public int BytesBefore(byte separator)
        {
            int num = 0;
            long position = this._memoryBuf.Position;
            while (this._memoryBuf.Position < this._memoryBuf.Length)
            {
                int num3 = this._memoryBuf.ReadByte();
                if (num3 == -1)
                {
                    return -1;
                }
                if (((byte) num3) == separator)
                {
                    this._memoryBuf.Position = position;
                    return num;
                }
                num++;
            }
            this._memoryBuf.Position = position;
            return 0;
        }

        public void Dispose()
        {
            if (this._binaryWriter != null)
            {
                this._binaryWriter.Close();
            }
            if (this._memoryBuf != null)
            {
                this._memoryBuf.Close();
            }
            GC.SuppressFinalize(this);
        }

        public int ReadableBytes() => 
            ((int) (this._memoryBuf.Length - this._memoryBuf.Position));

        public byte ReadByte() => 
            ((byte) (this._memoryBuf.ReadByte() & 0xff));

        public int ReadBytes(byte[] data) => 
            this._memoryBuf.Read(data, 0, data.Length);

        public void Reset()
        {
            this._memoryBuf.Seek(0L, SeekOrigin.Begin);
        }

        public void SetInt(int index, int i)
        {
            this._binaryWriter.Seek(index, SeekOrigin.Begin);
            this._binaryWriter.Write(ToBytes(i));
        }

        public void Skip(int bytes)
        {
            this._memoryBuf.Position += bytes;
        }

        public byte[] ToArray() => 
            this._memoryBuf.ToArray();

        private static byte[] ToBytes(int value)
        {
            byte[] buffer1 = new byte[4];
            buffer1[3] = (byte) value;
            buffer1[2] = (byte) (value >> 8);
            buffer1[1] = (byte) (value >> 0x10);
            buffer1[0] = (byte) (value >> 0x18);
            return buffer1;
        }

        public override string ToString()
        {
            byte[] bytes = this._memoryBuf.ToArray();
            int position = (int) this._memoryBuf.Position;
            return Encoding.UTF8.GetString(bytes, position, bytes.Length - position);
        }

        public void WriteByte(byte b)
        {
            this._binaryWriter.Write(b);
        }

        public void WriteByte(char c)
        {
            this._binaryWriter.Write((byte) (c & '\x00ff'));
        }

        public void WriteBytes(byte[] data)
        {
            this._binaryWriter.Write(data);
        }

        public void WriteBytes(byte[] data, int offset, int len)
        {
            this._binaryWriter.Write(data, offset, len);
        }

        public void WriteInt(int i)
        {
            this._binaryWriter.Write(ToBytes(i));
        }
    }
}

